﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model.Auth;
using Backend.JYD.Cache;

namespace Backend.JYD.Business
{
    public class AuthManager
    {
        #region const

        private const string key_AuthorityInfo = "AuthorityInfo_All";

        #endregion

        #region DAL

        private static readonly IDAL.Auth.IAuthorityInfo authDAL = Factory.DALFactory.CreateEFDAL<IDAL.Auth.IAuthorityInfo>("DAL.AuthorityInfoDAL");
        private static readonly IDAL.IRoleInfo roleDAL = Factory.DALFactory.CreateEFDAL<IDAL.IRoleInfo>("DAL.RoleInfoDAL");
        private static readonly IDAL.IEmpInfo empDAL = Factory.DALFactory.CreateEFDAL<IDAL.IEmpInfo>("DAL.EmpInfoDAL");

        #endregion

        private static Dictionary<Int64, List<AuthorityInfo>> EmpAuthorityInfoes = new Dictionary<long, List<AuthorityInfo>>();

        public List<AuthorityInfo> LoadAllAuthorityInfo()
        {
            var list = CacheMgr.Instance.GetCachedItem<List<AuthorityInfo>>(key_AuthorityInfo);
            if (list == null)
            {
                var temp = authDAL.GetAllAuthorityInfo();
                if (temp != null)
                {
                    list = temp.ToList();
                    CacheMgr.Instance.SetCachedItem<List<AuthorityInfo>>(key_AuthorityInfo, list);
                }
            }
            return list;
        }

        /// <summary>
        /// 增加，删除，修改权限时。
        /// </summary>
        public  void ClearEmpAuthorityInfoes()
        {
            new MenuManager().ClearEmpMenuInfoes();
            EmpAuthorityInfoes.Clear();
            var temp = authDAL.GetAllAuthorityInfo();
            if (temp != null)
            {
                CacheMgr.Instance.SetCachedItem<List<AuthorityInfo>>(key_AuthorityInfo, temp.ToList());
            }
        }
        /// <summary>
        /// 维护某人的权限时
        /// </summary>
        /// <param name="empId"></param>
        public void ClearAuthorityInfoesByEmpId(Int64 empId)
        {
            new MenuManager().ClearMenuInfoesByEmpId(empId);
            EmpAuthorityInfoes.Remove(empId);
            var infos = this.LoadEmpAuthorityInfoes(empId);
            if (infos != null && infos.Count > 0)
            {
                EmpAuthorityInfoes.Add(empId, infos);
            }
        }

        private List<AuthorityInfo> LoadEmpAuthorityInfoes(Int64 empId)
        {
            //1，直接给用户的权限。
            //2，用户角色赋予的权限。
            var list = new List<AuthorityInfo>();
            var all = this.LoadAllAuthorityInfo();
            if(all == null) return null;

            var empInfo = empDAL.GetEmpInfoByEmpId(empId);
            if (empInfo != null && empInfo.EmpAuthInfoes != null)
            {
                list.AddRange(all.FindAll(p => p.Status >0 && empInfo.EmpAuthInfoes.ToList().Exists(p1 => p1.Status > 0 && p1.AuthId == p.AuthId)));
            }
            if (empInfo != null && empInfo.EmpRoleInfoes != null)
            {
                var roleIds = new List<Int64>();
                empInfo.EmpRoleInfoes.ToList().ForEach(p => roleIds.Add(p.RoleId));
                var allRoles = new RoleManager().GetRoleInfoByRoleIds(roleIds);
                foreach (var role in allRoles)
                {
                    if (role.RoleAuthInfoes != null)
                    {
                        list.AddRange(all.FindAll(p => p.Status > 0 && role.RoleAuthInfoes.ToList().Exists(p1 => p1.Status > 0 && p1.AuthId == p.AuthId)));
                    }
                }
            }
            list = list.Distinct<AuthorityInfo>().ToList();
            return list;
        }

        public List<AuthorityInfo> GetEmpAuthorityInfoesByEmpId(Int64 empId)
        {
            if (EmpAuthorityInfoes.ContainsKey(empId))
                return EmpAuthorityInfoes[empId];

            var infos = this.LoadEmpAuthorityInfoes(empId);
            if (infos != null && infos.Count > 0)
            {
                EmpAuthorityInfoes.Add(empId, infos);
            }
            return infos;
        }
    }
}
